import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/home/key_point/key_point_microcosmic/key_point_small_view.dart';
import 'package:leader_help/page/task/main/models/area/area.dart';
import 'package:leader_help/page/task/main/models/industry_model/industry_model.dart';
import 'package:leader_help/page/task/main/task/widgets/city_picker.dart';
import 'package:leader_help/page/task/main/task/widgets/dropdown.dart';
import 'package:leader_help/utils/component/dragable.dart';
import 'package:leader_help/utils/util.dart';
import 'index.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';

import 'widgets/industry_picker.dart';

class TaskController extends GetxController {
  TaskController();

  final state = TaskState();

  @override
  void onInit() async {
    super.onInit();
    _initData();
  }

  void _initData() {
    state.comTypes = [
      '不限',
      '有融资',
      '最具发展潜力(通过资本等因素综合分析得出)',
      '瞪羚企业',
      '独角兽',
      '龙头企业',
      '单项冠军',
      '专精特新',
      '专精特新小巨人'
    ];
    state.scopes = [
      '不限规模',
      '规上企业',
      '中型企业',
      '小型企业',
      '微型企业',
    ];
    state.carouselTexts = [
      '河南全省所有行业所有规模且有融资的企业2040家',
      '其中最有发展潜力企业 94家',
      '其中郑州33%、洛阳16%，占据全省一半',
      '河南全省所有行业所有规模且有融资的企业2040家',
      '其中最有发展潜力企业 94家',
      '其中郑州33%、洛阳16%，占据全省一半',
      '河南全省所有行业所有规模且有融资的企业2040家',
      '其中最有发展潜力企业 94家',
      '其中郑州33%、洛阳16%，占据全省一半',
    ];
  }

  void actionMenuTypes(int index) {
    if (state.activeIndex == index) {
      //收起
      state.activeIndex = -1;
    } else {
      //弹出
      state.activeIndex = index;
      switch (index) {
        case 0:
          showDropDownList(
              left: 15.r,
              right: 15.r,
              top: SizeFit.navBarHeight + 60.r - SizeFit.statusBarHeight,
              height: 300.h,
              needBarrierColor: true,
              selectedIndex: state.selectComTypeIndex,
              list: state.comTypes,
              onTap: (selectedIndex) {
                state.selectComTypeIndex = selectedIndex;
                update();
              }).then((value) {
            state.activeIndex = -1;
            update();
          });
          break;
        case 1:
          actionCityPickers();
          break;
        case 2:
          actionIndustryPickers();
          break;
        case 3:
          showDropDownList(
              left: 15.r,
              right: 15.r,
              top: SizeFit.navBarHeight + 60.r - SizeFit.statusBarHeight,
              height: 300.h,
              needBarrierColor: true,
              selectedIndex: state.selectScopeIndex,
              list: state.scopes,
              onTap: (selectedIndex) {
                state.selectScopeIndex = selectedIndex;
                update();
              }).then((value) {
            state.activeIndex = -1;
            update();
          });
          break;
        default:
          break;
      }
    }

    update();
  }

  void actionCityPickers() async {
    if (state.sourceAreaMod == null) {
      var val = await rootBundle.loadString('assets/jsons/area.json');
      var _json = json.decode(val);
      var _area = AreaModel.fromJson(_json);
      state.sourceAreaMod = _area;
      state.selectedMod.initData(_area);
    }
    await showCityPickers(
        left: 15.r,
        right: 15.r,
        top: SizeFit.navBarHeight + 60.r - SizeFit.statusBarHeight,
        height: 400.h,
        needBarrierColor: true,
        areaMod: state.sourceAreaMod!,
        selectedMod: state.selectedMod,
        onChoosed: (mod) {
          state.selectedMod = mod;
        });
    state.activeIndex = -1;
    update();
  }

  void actionIndustryPickers() async {
    if (state.industryList == null) {
      var val = await rootBundle.loadString('assets/jsons/industry.json');
      List _json = json.decode(val);
      var _industrys = _json.map((e) => IndustryModel.fromJson(e)).toList();
      state.industryList = _industrys;
    }
    await showIndustryPickers(
        left: 15.r,
        right: 15.r,
        top: SizeFit.navBarHeight + 60.r - SizeFit.statusBarHeight,
        height: 400.h,
        needBarrierColor: true,
        mds: state.industryList!,
        selectedMod: state.industrySelectedMod,
        onChoosed: (mod) {
          state.industrySelectedMod = mod;
        });
    state.activeIndex = -1;
    update();
  }

  void actionGrap() {
    Get.toNamed(RouterPath.grap_question);
  }

  void actionToPointSmall(int index) {
    Get.to(() => const KeyPointSmallPage());
  }

  void actionToComList() {
    Get.toNamed(RouterPath.com_list);
  }

  @override
  void onReady() async {
    super.onReady();
  }

  void backToOrigin() {}

  void switchMapType() async {}

  @override
  void onClose() {
    super.onClose();
  }

  @override
  void dispose() {
    super.dispose();
  }

  /// 创建完成回调
  void onBMFMapCreated(BMFMapController controller) {
    state.myMapController = controller;

    /// 地图加载回调
    state.myMapController.setMapDidLoadCallback(callback: () {
      print('mapDidLoad-地图加载完成');
      if (GetPlatform.isAndroid) {
        for (int i = 0; i < state.position.length; i++) {
          setMyCircle(i);
        }
      }
    });
  }

  /// 设置地图参数
  BMFMapOptions initMapOptions() {
    BMFMapOptions mapOptions = BMFMapOptions(
      showZoomControl: false,
      zoomEnabled: false,
      zoomEnabledWithDoubleClick: false,
      center: BMFCoordinate(34.72468, 113.6401), //郑州的经纬度
      zoomLevel: 10, //地图缩放等级
    );
    return mapOptions;
  }

  setMyCircle(int index) {
    /// 构造圆
    BMFCircle circle = BMFCircle(
        center: state.position[index],
        radius: 20000,
        width: 5,
        strokeColor: Color(0x55666666),
        fillColor: state.colors[index],
        lineDashType: BMFLineDashType.LineDashTypeSquare);

    /// 构造text
    BMFText bmfText = BMFText(
        text: '企业' + state.num[index],
        position: BMFCoordinate(state.position[index].latitude + 0.06,
            state.position[index].longitude),
        bgColor: Colors.transparent,
        fontColor: Colors.white,
        fontSize: 20,
        typeFace: BMFTypeFace(
            familyName: BMFFamilyName.sSansSerif,
            textStype: BMFTextStyle.NORMAL),
        alignY: BMFVerticalAlign.ALIGN_CENTER_VERTICAL,
        alignX: BMFHorizontalAlign.ALIGN_CENTER_HORIZONTAL,
        rotate: 0.0);

    /// 构造text
    BMFText bmfText2 = BMFText(
        text: '占比' + state.ratio[index],
        position: state.position[index],
        bgColor: Colors.transparent,
        fontColor: Colors.white,
        fontSize: 20,
        typeFace: BMFTypeFace(
            familyName: BMFFamilyName.sSansSerif,
            textStype: BMFTextStyle.NORMAL),
        alignY: BMFVerticalAlign.ALIGN_CENTER_VERTICAL,
        alignX: BMFHorizontalAlign.ALIGN_CENTER_HORIZONTAL,
        rotate: 0.0);

    /// 构造text
    BMFText bmfText3 = BMFText(
        text: state.city[index],
        position: BMFCoordinate(state.position[index].latitude - 0.06,
            state.position[index].longitude),
        bgColor: Colors.transparent,
        fontColor: Colors.white,
        fontSize: 20,
        typeFace: BMFTypeFace(
            familyName: BMFFamilyName.sSansSerif,
            textStype: BMFTextStyle.NORMAL),
        alignY: BMFVerticalAlign.ALIGN_CENTER_VERTICAL,
        alignX: BMFHorizontalAlign.ALIGN_CENTER_HORIZONTAL,
        rotate: 0.0);

    state.myMapController.addCircle(circle);
    state.myMapController.addText(bmfText);
    state.myMapController.addText(bmfText2);
    state.myMapController.addText(bmfText3);
  }
}
